class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int ret = -1;
        int y = 0;
        for (auto e : nums)
            y += e;

        int target = y - x;
        int sum = 0;
        if (target < 0)    return -1;
        for (int left = 0, right = 0; right < nums.size(); right++)
        {
            sum += nums[right];
            while (sum > target)
                sum -= nums[left++];
            if (sum == target)
                ret = max(ret, right - left + 1);
        }

        return ret == -1 ? -1 : nums.size() - ret;
    }
};